package com.guangfeng.yql.comm.netty.util;

import com.guangfeng.yql.comm.netty.chatroom.ChatRoomMessageQueue;
import com.guangfeng.yql.comm.netty.constant.Constant;
import com.guangfeng.yql.redis.util.RedisCacheUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class ChatRoomUtil {

    private static Map<Integer, ChannelGroup> chatRoomChannel = new HashMap<>();
    // 链接是否在 房间中
    private static Map<ChannelId, Integer> channelChatRoom = new HashMap<>();

    private static ChannelGroup totalChannel = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    private static ChannelGroup unwantedChannel = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    private static ChannelGroup getChannelGroup() {
        return new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    }

    public static void append(Channel channel) {
        totalChannel.add(channel);
        unwantedChannel.add(channel);
    }

    public static void delete(ChannelHandlerContext channelHandlerContext) {
        Channel channel = channelHandlerContext.channel();
        quitChatRoom(channel);
        channelChatRoom.remove(channel.id());
        channel.close();
        channelHandlerContext.close();
    }

    public static void cdChatRoom(Channel channel, Integer chatRoom) {
        unwantedChannel.remove(channel);
        if (!chatRoomChannel.containsKey(chatRoom)) {
            chatRoomChannel.put(chatRoom, getChannelGroup());
        }
        ChannelGroup group = getChatRoomChannel(chatRoom);
        group.add(channel);
        channelChatRoom.put(channel.id(), chatRoom);
        RedisCacheUtil.appendCacheSet(Constant.CHAT_ROOM_KEY, chatRoom);
        ChatRoomMessageQueue.append(chatRoom.toString(), NettyResponseBeanUtil.text("进入直播间", "张三"));
    }

    public static void quitChatRoom(Channel channel) {
        Integer chatRoom = channelChatRoom.get(channel.id());
        if (chatRoom == null) {
            return;
        }
        unwantedChannel.add(channel);
        if (!chatRoomChannel.containsKey(chatRoom)) {
            chatRoomChannel.put(chatRoom, getChannelGroup());
        }
        ChannelGroup group = getChatRoomChannel(chatRoom);
        group.remove(channel);
        channelChatRoom.remove(channel.id());
        ChatRoomMessageQueue.append(chatRoom.toString(), NettyResponseBeanUtil.text("退出直播间", "张三"));
    }

    public static ChannelGroup getChatRoomChannel(Integer chatRoom) {
        return chatRoomChannel.get(chatRoom);
    }

    public static Map<Integer, ChannelGroup> getChatRoomChannelAll() {
        Set<Integer> set = RedisCacheUtil.getCacheSet(Constant.CHAT_ROOM_KEY);
        Map<Integer, ChannelGroup> map = new HashMap<>(set.size());
        set.forEach(key -> {
            map.put(key, chatRoomChannel.get(key));
        });
        return map;
    }

    public static void sendText(Channel channel, String message) {
        Integer chatRoom = channelChatRoom.get(channel.id());
        ChatRoomTextMessageSendUtil.sendMessageCollection(NettyResponseBeanUtil.text(message, "张三"), getChatRoomChannel(chatRoom));
    }
}
